package models.dao;

import java.sql.*;

//Classe que faz a conexão com o banco de dados

public class DAOConexao {
	private static Connection connection = null;
 	
	public static Connection getConnection()
    {
		// Registro do Driver do SqlServer para ser reconhecido pela classe DriveManager 
		String classWithNamespace = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		
		try{
			Class.forName(classWithNamespace);
			
		}catch(ClassNotFoundException cnfe){
			System.out.println("Error loading driver: " + cnfe);
		}
		
		// Dados da conexão
		String url = "jdbc:sqlserver://";
		String serverName= "localhost";
		String portNumber = "1433";
		String databaseName= "General";
		String username = "sa";
		String password = "senha";
		String selectMethod = "cursor"; 
		String connectionUrl = url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";"; 	
		//jdbc:sqlserver://localhost:1433;databaseName=Estigaa;cursor;
		
		try {
			// Objeto de conexão
			connection = DriverManager.getConnection(connectionUrl, username, password);
		}
		catch (SQLException e){
			System.out.println("Erro abrindo conexão: " + e);
		}	
		return connection;
    }

	public static void closeConnection(){
		try{
			// Fecha conexão
			if (connection != null) connection.close();
			
		}catch (SQLException e){
			System.out.println("Erro fechando conexão: " + e);
		}
	}
	/*public static void main(String[] args) {
		try{
			System.out.println(daoConexao.getConnection().prepareStatement("select from * Professor"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}*/
}
